package com.cnfangmao.masterdata.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cnfangmao.masterdata.model.Role;
import com.cnfangmao.masterdata.model.RolePermission;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dao.AccountRoleDao;
import com.cnfangmao.masterdata.dao.RoleDao;
import com.cnfangmao.masterdata.dao.RolePermissionDao;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.dto.RoleDto;
import com.cnfangmao.masterdata.dto.RoleSearchDto;
import com.cnfangmao.masterdata.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-17
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private RolePermissionDao rolePermissionDao;
	
	@Autowired
	private AccountRoleDao accountRoleDao;

	@Override
	public boolean save(RoleDto dto, String createdBy) {
		boolean result = false;
		Role entityRole = new Role();
		entityRole.setRoleName(dto.getRoleName());
		entityRole.setRoleCode(dto.getRoleCode());
		entityRole.setRoleId(SnowFlakeUtils.generator());
		entityRole.setRemark(dto.getRemark());
		entityRole.setCreatedOn(new Date());
		entityRole.setCreatedBy(createdBy);
		result = roleDao.insert(entityRole) > 0;
		if (result) {
			List<String> rolePermission = dto.getRolePermission();
			for (String id : rolePermission) {
				RolePermission rolePermissionEntity = new RolePermission();
				rolePermissionEntity.setRoleId(entityRole.getRoleId());
				rolePermissionEntity.setPermissionId(id);
				rolePermissionEntity.setRolePermissionId(SnowFlakeUtils
						.generator());
				rolePermissionEntity.setCreatedOn(new Date());
				rolePermissionEntity.setCreatedBy(createdBy);
				rolePermissionDao.insert(rolePermissionEntity);
			}

		}
		return result;
	}

	@Override
	public boolean update(RoleDto dto, String modifiedBy) {
		boolean result = false;
		Role role = roleDao.selectById(dto.getRoleId());
		role.setRoleName(dto.getRoleName());
		role.setRoleCode(dto.getRoleCode());
		role.setRemark(dto.getRemark());
		role.setCreatedOn(new Date());
		role.setCreatedBy(modifiedBy);
		result = roleDao.updateById(role) > 0;
		if (result) {
			List<String> rolePermission = dto.getRolePermission();
			rolePermissionDao.deleteRolePermission(role.getRoleId());
			for (String id : rolePermission) {
				RolePermission rolePermissionEntity = new RolePermission();
				rolePermissionEntity.setRoleId(role.getRoleId());
				rolePermissionEntity.setPermissionId(id);
				rolePermissionEntity.setRolePermissionId(SnowFlakeUtils
						.generator());
				rolePermissionEntity.setCreatedOn(new Date());
				rolePermissionEntity.setCreatedBy(modifiedBy);
				rolePermissionDao.insert(rolePermissionEntity);
			}
		}
		return result;
	}

	@Override
	public boolean delete(String roleId) {
		boolean result = false;
		result = roleDao.deleteById(roleId) > 0;
		if (result) {
			rolePermissionDao.deleteRolePermission(roleId);
			accountRoleDao.deleteAccountRoleByRoleId(roleId);
		}
		return result;
	}

	@Override
	public IPage<Role> queryListRole(GeneralSearchDto<RoleSearchDto> dto) {
		IPage<Role> page = roleDao.selectPage(
				new Page<>(dto.getPageNo(), dto.getPageSize()),
				new QueryWrapper<Role>()
						.like(StringUtils.isNotEmpty(dto.getConditions().getRoleName()),
								"role_name", dto.getConditions().getRoleName())
						.or()
						.like(StringUtils.isNotEmpty(dto.getConditions().getRoleCode()),
								"role_name", dto.getConditions().getRoleCode()));

		return page;
	}

	@Override
	public List<Role> getAllRoles() {
		List<Role> rolePermissionList = new ArrayList<Role>();
			       rolePermissionList = roleDao.getAllRoles();
		return rolePermissionList;
	}

	@Override
	public Role findByRoleName(String roleName) {
		Role role = null;
			 role = roleDao.findByRoleName(roleName);
		return role;
	}

}
